const mapDispatchToProps={
    increase:()=>{
      return {
        type:"INCREASE"
      }
    },
    decrease:()=>{
      return {
        type:"DECREASE"
      }
    },
    falseLogin:()=>{
      return {
        type:"FALSELOGIN"
      }
    },
    getListData:(data)=>{
      return {
        type:"GETLIST",
        data
      }
    },
    addToCartFunc:(data)=>{
      return {
        type:"ADDTOCART",
        data
      }
    },
    regSuccess:()=>{
      return {
        type:"REGISTERTRUE",
      }
    },
    changeRegester:function(){
      return {
        type:"REGISTERFALSE",
      }
    },
    UpdataStoreData:(data)=>{
      
      return {
        type:"UPDATACARTS",
        data
      }
    },
    addToCart:(params)=>{//汪store里面添加包含/更新数量的购物信息
         return dispatch=>{
           dispatch(mapDispatchToProps.addToCartFunc(params))
         }
    },
    changeUseName:(data)=>{
      return {
        type:"CAHNGNAME",
        data
      }
    },
    fetchList:function(params={page:1}){
      var start=20*(params.page-1)
    return dispatch=>{
      return fetch('/api/v2/movie/top250?start='+start)
      .then(res=>{     
        return res.json()
      })
      .then(data=>{        
        dispatch(mapDispatchToProps.getListData(data))
      })
    }
    },
    addToDatabaceCart(goodmessage){//从商品页面添加购物产品到数据库，操作完成
      console.log("goodmessage",goodmessage)
      return dispatch=>{
      return fetch('http://localhost:3000/product/data',{
        method: "POST",
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        body:"title="+goodmessage.title+"&price="+goodmessage.year+"&genders="+goodmessage.genres+"&num="+goodmessage.num+"&src="+goodmessage.images.large
      })
      .then(res=>{     
        return res.json()
      })
      .then(data=>{
        dispatch(mapDispatchToProps.getDatabaselist())
      })
    }
    },
    fromCartAddToDatabaceCart(goodmessage){//从购物车页面添加购物产品到数据库，操作完成
      console.log("goodmessage",goodmessage)
      return dispatch=>{
      return fetch('http://localhost:3000/product/data',{
        method: "POST",
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        body:"title="+goodmessage.title+"&price="+goodmessage.price+"&genders="+goodmessage.genders+"&num="+goodmessage.num+"&src="+goodmessage.src
      })
      .then(res=>{     
        return res.json()
      })
      .then(()=>{
        dispatch(mapDispatchToProps.getDatabaselist())
      })
    }
    },
    goodDelete(goodmessage){
      return dispatch=>{
        return fetch('http://localhost:3000/product/data/'+goodmessage,{
          method: "DELETE",
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        })
        .then(res=>{     
          return res.json()
        })
        .then((data)=>{
         
         dispatch(mapDispatchToProps.getDatabaselist())
        })
      }
    },
    getDatabaselist(){//获取数据库里的商品信息
      return dispatch=>{
        return fetch('http://localhost:3000/product/list',{
          method: "POST",
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        })
        .then(res=>{    
          return res.json()
        }).then(data=>{       
         dispatch(mapDispatchToProps.UpdataStoreData(data.docs))
        })
      }
    },
    sendUserMassage:function(massage={email:0,password:0}){
      return dispatch=>{
        return fetch('http://localhost:3000/users/login',{
          method: "POST",
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          body:"email="+massage.email+"&password="+massage.password
        })
        .then(res=>{   
           
          return res.json()
        })
        .then(data=>{ 
          if(data.massage=="邮箱或者密码错误！") {
            alert("邮箱或密码输入错误")
          } else{
           // location.href="http://localhost:8080/"
             dispatch(mapDispatchToProps.changeUseName(data))
          }             
        })
      }  
    },
    usesRegister:function(massage={email:0,password:0,name:"22"}){
      return dispatch=>{
        return fetch('http://localhost:3000/users/data',{
          method: "POST",
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          body:"email="+massage.email+"&password="+massage.password+"&name="+massage.name
        })
        .then(res=>{     
          return res.json()
        })
        .then(data=>{        
          if(data.massage=="邮箱已经被占用") {  

          } else if(data.massage=="用户名已经被占用"){  
                     
          } else{          
            dispatch(mapDispatchToProps.regSuccess())
          }      
        })
      }  
    }
    
    
}

  export default mapDispatchToProps;